package com.dataart.productshop.controller;

import com.dataart.productshop.Constants;
import com.dataart.productshop.model.FileInfo;
import com.dataart.productshop.model.Picture;
import com.dataart.productshop.model.boiko.informationsystem.Lecturer;
import com.dataart.productshop.model.boiko.informationsystem.Student;
import com.dataart.productshop.service.FileInfoManager;
import com.dataart.productshop.service.LecturerManager;
import com.dataart.productshop.service.StudentManager;
import com.dataart.productshop.utils.PictureBlock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.text.DateFormat;
import java.util.*;

/**
 * Handles requests for the application home page.
 */
@Controller
public class MainController {
	
	private static final Logger logger = LoggerFactory.getLogger(MainController.class);

    @Autowired
    FileInfoManager fileInfoManager;


    @Autowired
    private LecturerManager lecturerManager;

    @Autowired
    private StudentManager studentManager;


    //can be changed in property file
    @Value("default.image")
    String defaultImageUrl;
	
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Locale locale, Model model) {

		return "home";
	}

    @RequestMapping(value = "/", method = RequestMethod.POST)
    public ModelAndView home(@RequestParam(value = "searchName", required = false)String query) {
        if(query == null || query.isEmpty()){
            return new ModelAndView("home");
        }

        System.out.println("enter with == " + query);
        List<Student> sts = studentManager.getStudentByNameQuery(query);
        List<Lecturer> lecturers = lecturerManager.getLecturerByNameQuery(query);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("students", sts);
        map.put("lecturers", lecturers);
        map.put("result", 1);
        map.put("query", query);


        return new ModelAndView("home", map);
    }
	
}
